/*
 *@author 
 *
 *E-mail: cliffle007@163.com 
 */
package util;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;



public class DataUtil {

	
	public static int XOR(byte[]data,int len)
	{
		byte chk;
		chk= data[0];
		for (int i=1;i<len;i++)
		{
			chk=(byte) (chk ^ data[i]);
		}
		return (chk & 0xff);
		
	}
	
	public static int LRC(byte[] data,int istart, int iend) {
		int uchLRC = 0;
		for (int i = istart; i <= iend; i++) {
			uchLRC = uchLRC + (data[i] & 0xff);
		}
		uchLRC= uchLRC % 256;
		int LRC = 0xff - uchLRC + 1;
		return LRC;
	}
	
	public static int LRC(byte[] data, int data_length) {
		int uchLRC = 0;
		for (int i = 0; i < data_length; i++) {
			uchLRC = uchLRC + (data[i] & 0xff);
		}
		uchLRC= uchLRC % 256;
		int LRC = 0xff - uchLRC + 1;
		return LRC;
	}


	public static int[] auchCRCHi = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80,
			0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
			0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
			0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
			0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
			0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
			0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
			0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
			0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
			0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
			0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00,
			0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
			0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
			0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
			0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00,
			0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
			0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
			0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
			0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00,
			0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
			0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80,
			0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
			0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
			0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 };
	public static int[] auchCRCLo = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02,
			0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C,
			0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9,
			0x09, 0x08, 0xC8, 0xD8, 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x5F, 0x9F, 0x9E, 0x5E,
			0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49,
			0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D,
			0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82,
			0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 };

	public static int CRC16(byte[] data, int data_length) {
		int i = 0;
		int uIndex = 0;
		int uchCRCHi = 0xFF;
		int uchCRCLo = 0xFF;
		while (data_length > 0) {
			uIndex = uchCRCHi ^ (data[i] & 0xff);
			uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex];
			uchCRCLo = auchCRCLo[uIndex];
			data_length = data_length - 1;
			i = i + 1;
		}
		return ((uchCRCHi << 8) | (uchCRCLo));
	}



	public static byte[] HexStrToBytes(String HexStr) {
		int Dlength = HexStr.length() / 2;
		byte[] ret = new byte[Dlength];
		for (int i = 0; i < Dlength; i++) {
			String str = HexStr.substring(i * 2, i * 2 + 2);
			ret[i] = (byte) Integer.valueOf(str, 16).intValue();
		}
		return ret;
	}

	public static String BytesToHexStr(byte[] s) {
		String res = "";
		for (int i = 0; i < s.length; i++) {
			res = res + String.format("%02X", s[i] & 0xff);
		}
		return res;

	}
	public static byte[] str2Bcd(String asc) {
		int len = asc.length();
		int mod = len % 2;

		if (mod != 0) {
			asc = "0" + asc;
			len = asc.length();
		}

		if (len >= 2) {
			len = len / 2;
		}

		byte bbt[] = new byte[len];
		byte abt[] = asc.getBytes();
		int j, k;

		for (int p = 0; p < asc.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}

			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}

			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	} 
	public static int  crc_table[] =
	{
		0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
		0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
		0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
		0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
		0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
		0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
		0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
		0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
		0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
		0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
		0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
		0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
		0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
		0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
		0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
		0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
		0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
		0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
		0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
		0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
		0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
		0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
		0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
		0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
		0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
		0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
		0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
		0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
		0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
		0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
		0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
		0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
	};

	public static int  gen_crc(byte[] buffer, int buffer_length)
	{
		int crc = 0;
		int i;

		for (i = 0; i < buffer_length; i++)
			crc = crc_table[((crc >> 8) ^ (buffer[i]& 0xff) ) & 0xFF] ^ (crc << 8);
		
		return crc & 0xffff;
	}
	
	public static void main(String[] args) {
//		final String s1 = 
//			"22 03 48 4b 1a 00 00 04 00 00 00 00 14 00 03 00 00 00 00 00 00 00 00 00 00 fc fc 00 fc fc 00 00 00 00 00 00 00 00 00 fc fc 00 fc fc 00 00 00 00 00 00 00 00 00 fc fc 00 fc fc 64 00 00 00 00 00 00 00 00 fc fc fe fc 00 00 00 46 f6 00";
//		
//		
//		final String s3 
//		=
//		"1d 03 66 a4 1c 00 00 04 05 00 00 00 14 00 03 00 00 00 00 00 04 00 00 00 00 fc 39 00 fc 39 05 00 33 00 05 00 00 00 00 fc 37 00 fc 37 0a 00 2a 00 07 00 00 00 00 fc 36 00 fc 36 0c 00 23 00 07 00 00 00 00 fc 42 00 fc 42 09 00 1c 00 02 20 03 00 68 00 02 1b 04 00 6a 00 04 16 08 00 4f 00 01 0d 03 00 a3 00 02 4b 03 00 3f f2 8d";
//		
//		final String s4 = 
//		"25 03 52 90 11 00 00 03 04 00 00 00 14 00 03 00 00 00 00 00 00 00 00 00 00 fc fc 00 fc fc 00 00 00 00 01 00 00 00 00 fc 4d 00 fc 4d 01 00 8a 00 03 00 00 00 00 fc 61 00 fc 61 03 00 42 00 06 1c 0a 00 27 00 02 1a 04 00 63 00 04 1e 06 00 34 00 02 1b 04 00 5d 29 3b";
//		
//	    final String s5 = "30 31 32 33 34 35 36 37";
//		
//		String s = s5;
//		
//		String[] ss = s.split(" ");
//		byte[] bb = new byte[ss.length];
//		for (int i = 0;i<bb.length;i++){
//			bb[i] = (byte)Integer.parseInt(ss[i],16);
//		}
//		
//		int d = DataUtil.CRC16(bb,bb.length-2);
//		System.out.println(Integer.toHexString(d));
		
		//DataUtil.
		float f=(float) 4.39;
		//byte[] b= floatToByte(f);
		byte[] b= float2byte(f);
		String s1= BytesToHexStr(b);
		System.out.println(s1);
		String s= "408C7AE1";
		float v=getFloatValue(HexStrToBytes(s));
		System.out.println(v);
		
		
	}
	
	public static byte[] SumChk(byte[]data){
		byte[] res= new byte[2];
		int j=0;
		for(int i=0;i<data.length;i++){
			j=j+data[i]&0xff;
		}
		res[0]=(byte)(j /256);
		res[1]=(byte)(j %256);
		return res;
			
	}
	
	public static byte[] genbytes(String tb){
		ByteArrayOutputStream op= new ByteArrayOutputStream();
		String[] sts =tb.split(", ");
		for (int i=0;i<sts.length;i++){
			op.write(Integer.parseInt(sts[i]));
		}
		return op.toByteArray();
		
	}
	
	public  static String RFillWith(String obj  ,String c,int len){
		
		while (obj.length()<len){
			obj=obj+c;
		}
		return obj;
	}
	public  static String LFillWith(String obj  ,String c,int len){
		
		while (obj.length()<len){
			obj=c+obj;
		}
		return obj;
	}
	public static byte[] RFillWith(byte[]bts,int len,int b){
		if (bts.length>len)
			return bts;
		byte[] rbts=new byte[len];
		for(int i=0;i<len;i++){
			rbts[i]=(byte)b;
		}
		System.arraycopy(bts, 0, rbts, 0, bts.length);
		return rbts;
		
	}
	public static byte[] floatToByte(float v) {
        ByteBuffer bb = ByteBuffer.allocate(4);
        byte[] ret = new byte [4];
        FloatBuffer fb = bb.asFloatBuffer();
        fb.put(v);
        bb.get(ret);
        return ret;
    }
	public static byte[] short2byte(int i){
		int hi= i /256;
		int lo=i%256;
		return new byte[] {(byte)hi,(byte)lo}; 
	}
	public static byte[] int2byte(int i){
		int hi= i / 65536;
		int hihi = hi/256;
		int hilo= hi%256;
		int low= i % 65536;
		int lohi= low /256;
		int lolo=low%256;
		
		
		return new byte[] {(byte)hihi,(byte)hilo,(byte)lohi,(byte)lolo};
	}

	public static byte[] reverse(byte[] b) {
		int left = 0; // index of leftmost element
		int right = b.length - 1; // index of rightmost element

		while (left < right) {
			// exchange the left and right elements
			byte temp = b[left];
			b[left] = b[right];
			b[right] = temp;

			// move the bounds toward the center
			left++;
			right--;
		}
		return b;
	}// endmethod reverse
	
	
	//iec104 correct method
	public static byte[] float2byte(float f) {   
	      
	    // 把float转换为byte[]  
	    int fbit = Float.floatToIntBits(f);  
	      
	    byte[] b = new byte[4];    
	    for (int i = 0; i < 4; i++) {    
	        b[i] = (byte) (fbit >> (24 - i * 8));    
	    }   
	      
	    // 翻转数组  
	    int len = b.length;  
	    // 建立一个与源数组元素类型相同的数组  
	    byte[] dest = new byte[len];  
	    // 为了防止修改源数组，将源数组拷贝一份副本  
	    System.arraycopy(b, 0, dest, 0, len);  
	    byte temp;  
	    // 将顺位第i个与倒数第i个交换  
	    for (int i = 0; i < len / 2; ++i) {  
	        temp = dest[i];  
	        dest[i] = dest[len - i - 1];  
	        dest[len - i - 1] = temp;  
	    }  
	      
	    return dest;  
	      
	}  
	public static float getFloatValue(int lo, int hi) {
		int value = (hi << 16) | lo;
		return Float.intBitsToFloat(value);
	}
	public static float getFloatValue(byte[] src){
		int low= (src[0]&0xff)+(src[1]&0xff)*256;
		int hi=(src[2]&0xff)+(src[3]&0xff)*256;
		return getFloatValue(low,hi);
	}

	

}
